package com.abuaziz.librarys.Notification

import android.app.Notification
import android.app.PendingIntent
import android.content.Intent
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.os.VibrationEffect
import androidx.appcompat.app.AppCompatActivity
import com.abuaziz.librarys.R
import com.pawegio.kandroid.notificationManager
import com.pawegio.kandroid.vibrator
import com.ug_project.Async.runASync
import com.ug_project.views.click
import kotlinx.android.synthetic.main.activity_notification.*
import me.ezitku.base.extensions.Notification.createNotification
import me.ezitku.base.extensions.Notification.createNotificationChannel
import me.ezitku.base.extensions.Notification.createNotificationGroup
import java.io.File

class NotificationTest : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_notification)

//        val chanels = notificationManager?.notificationChannels
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val chanels = notificationManager?.notificationChannels
            val groups = notificationManager?.notificationChannelGroups
            chanels?.forEach {
                notificationManager?.deleteNotificationChannel(it.id)
            }
            groups?.forEach {
                notificationManager?.deleteNotificationChannelGroup(it.id)
            }

        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationManager?.deleteNotificationChannel("0")
            notificationManager?.deleteNotificationChannel("3")
        }


        create.click {
            createNotification(R.drawable.ic_launcher_background) {
                _notifyId = 1
                this._channelId = "1"
                _channeName = "测试名称7"
                _autoCancel = false
                _noClear = false
                this._enable_lights = true
                this._enable_vibration = true
                _titleText = "艾孜孜"
                _contentText = "yahximu \n\t\r abc"

                this._pendingIntent = PendingIntent.getActivity(this@NotificationTest, 0,
                        Intent(this@NotificationTest, NotificationTest::class.java), PendingIntent.FLAG_IMMUTABLE)

            }
        }

        //创建有声音通知
        create_with_sound.click {

//            val ringtoneManager  = RingtoneManager(this)
//            val cursor = ringtoneManager.cursor



            createNotificationChannel("1234567", "有声音通知1234567") {
                val f = File(Environment.getExternalStorageDirectory().absolutePath + File.separator + "ringtones/_dingdong.mp3")
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    it.enableLights(false)
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    it.enableVibration(false)
//                    it.vibrationPattern = longArrayOf(0, 180, 80, 120)
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                    it.setSound(Uri.fromFile(f),null)
                    it.setSound(null, null)
                }
            }

            createNotification(R.mipmap.ic_launcher_round, 1234, "1234") {
                it.setContentTitle("有声音通知内容")
                it.setContentText("有声音通知")
            }

        }

        create_bigtext.click {
            createNotification(R.mipmap.ic_launcher_round) {
                this._notifyId = 3
                this._channelId = "3"
                this._channeName = "大量内容通知"
                this._titleText = "你有一条消息"
                this._contentText = "长按打开"
                this._bigContent = Notification.BigTextStyle().apply {
                    this.bigText("多行文字内容，多行文字内容，多行文字内容，多行文字内容，多行文字内容，多行文字内容，多行文字内容，多行文字内容，多行文字内容，多行文字内容，多行文字内容")
                    this.setBigContentTitle("大量内容标题")
                }
            }
        }


//        createNotificationGroup("group_id", "group_name")
//        createNotificationChannel("4", "多行内容通知") {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                it.group = "group_id"
//            }
//        }

        var notifyId = 0
        create_multiline_text.click {


            val lineText = arrayListOf("你好，朋友", "你是谁？", "你是哪里人")
            createNotification(R.mipmap.ic_launcher_round, ++notifyId, "4") {
                this._titleText = "艾孜孜"
                this._contentText = "[${lineText.size}条]${this._titleText}:${lineText[0]}"
                this._multiLineContent = Notification.InboxStyle().apply {
                    lineText.forEach { this.addLine(it) }
                    this.setBigContentTitle("多行内容标题")
                    this.setSummaryText("[${lineText.size}条]${this@createNotification._titleText}")
                }
//                this.group ="group_name"
//                this.groupSummary = true
            }


//            createNotification(R.mipmap.ic_launcher_round,++notifyId,"4") {
//                this._titleText = "Eleven"
//                this._contentText = "[${lineText.size}条]${this._titleText}:${lineText[0]}"
//                this._multiLineContent = Notification.InboxStyle().apply {
//                    lineText.forEach { this.addLine(it) }
//                    this.setBigContentTitle("多行内容标题 ELeven")
//                    this.setSummaryText("[${lineText.size}条]${this@createNotification._titleText}")
//                }
//                this.group ="group_name"
//                this.groupSummary = false
//            }
        }


        create_two.click {
            runASync {
                for (i in 1..100) {
                    Thread.sleep(100)
                    runUI {
                        createNotification(R.drawable.ic_launcher_background) {
                            _channeName = "测试名称"
                            _notifyId = 3
                            _titleText = "正在下载中... $i%"
                            _contentText = "请稍等..."
                            setProgress(100, i)
                        }
                    }
                }
            }


        }
        cancel.click {
            notificationManager?.cancel(1)
            notificationManager?.cancel(3)
        }
    }

}